/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.com.smart.hawk.model.caller;

import co.com.smart.connection.model.manager.JDBCResourceManager;
import co.com.smart.hawk.dto.*;
import co.com.smart.hawk.utilities.HawkSessionMgr;
import java.math.BigDecimal;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Andres Felipe Escobar Lopez
 */
public class ArticulosCaller extends JDBCResourceManager{

    public ArticulosCaller() {
        super();
    }
    
    /**
     * Almacena los articulos
     * @param articulo
     * @return
     * @throws Throwable 
     */
    public ArticulosDTO grabarArticulos(ArticulosDTO articulo)throws Throwable{
        ArticulosDTO returnObject = new ArticulosDTO();
        
        Connection conn = null;
        CallableStatement call = null;
        UsuarioDTO user = (UsuarioDTO)HawkSessionMgr.getInstance().getSession("USER_SESSION");
        
        try {
            
            conn = getConnection();
            call = conn.prepareCall("{call GrabarArticulos(?,?,?,?,?,?,?,?,?,?,?)}");
            call.setString(1, articulo.getCoarticulo());
            call.setString(2, articulo.getDsarticulo());
            call.setString(3, articulo.getDsarticulocorto());
            call.setBigDecimal(4, articulo.getMarcas().getScdatmaestro());
            call.setBigDecimal(5, articulo.getTipoArticulo().getScdatmaestro());
            call.setBigDecimal(6, articulo.getUnidadMedida().getScdatmaestro());
            call.setBigDecimal(7, articulo.getNmvalorventa());
            if(articulo.getIva() != null){
                call.setBigDecimal(8, articulo.getIva().getScdatmaestro());
            }else{
                call.setInt(8, java.sql.Types.NULL);
            }
            call.setString(9, user.getCocodigo());
            call.registerOutParameter(10, java.sql.Types.VARCHAR);
            call.registerOutParameter(11, java.sql.Types.VARCHAR);
            call.executeUpdate();
            returnObject.setDsmensaje(call.getString(10));
            returnObject.setCodigo(call.getString(11));
            
        } catch (SQLException e) {
            throw  e;
        }finally{
            closeResources(conn, call);
        }
        
        
        return returnObject;
    }
    
  public List<ArticulosDTO> ListarArticulos(ArticulosDTO filtros)throws Throwable{
        List<ArticulosDTO> returnArticulos = new ArrayList<>();
        Connection conn = null;
        CallableStatement call = null;
        ResultSet rs = null;
        try{
            conn = getConnection();
            call = conn.prepareCall("{call consultarArticulos()}");
           // call.setString(1, filtros.getCoempresa());
            rs = call.executeQuery();
            
            if (rs!=null){
                while(rs.next()){
                    ArticulosDTO articulos = new ArticulosDTO();
                    DatosMaestrosDTO marcas = new DatosMaestrosDTO();
                    DatosMaestrosDTO tipoarticulo = new DatosMaestrosDTO();
                    DatosMaestrosDTO unidadmedida = new DatosMaestrosDTO();
                    DatosMaestrosDTO iva = new DatosMaestrosDTO();
                    
                    articulos.setScarticulo(rs.getBigDecimal("sm_scarticulo"));
                    articulos.setCoarticulo(rs.getString("sm_coarticulo"));
                    articulos.setDsarticulo(rs.getString("sm_dsarticulo"));
                    articulos.setDsarticulocorto(rs.getString("sm_dsarticulocorto"));
                    marcas.setScdatmaestro(rs.getBigDecimal("sm_scmarca"));
                    marcas.setDsdatmaestro(rs.getString("dsmarca"));
                    articulos.setMarcas(marcas);
                    tipoarticulo.setScdatmaestro(rs.getBigDecimal("sm_sctipoarticulo"));
                    tipoarticulo.setDsdatmaestro(rs.getString("dstipoarticulo"));                   
                    articulos.setTipoArticulo(tipoarticulo);
                    unidadmedida.setScdatmaestro(rs.getBigDecimal("sm_scunidadmedida"));
                    unidadmedida.setDsdatmaestro(rs.getString("dsunidadmedida"));
                    articulos.setUnidadMedida(unidadmedida);
                    iva.setScdatmaestro(rs.getBigDecimal("sm_sciva"));
                    iva.setDsdatmaestro(rs.getString("dsiva"));
                   articulos.setIva(iva);
                   articulos.setNmvalorventa(rs.getBigDecimal("sm_nmvalorventa"));
                  
                    returnArticulos.add(articulos);
                }
            }
        } catch (SQLException e) {
            throw  e;
        }finally{
            closeResources(conn, call, rs);
        }
        return returnArticulos;
    }  
    
}
